// lib/create.js

const path = require("path");
// fs-extra 是对 fs 模块的扩展，支持 promise 语法
const fs = require("fs-extra");
const inquirer = require("inquirer");
const ora = require("ora");
const spinner = ora();

const Generator = require("./Generator");

module.exports = async function create(name, options) {
  // 执行创建命令

  // 当前命令行选择的目录
  const cwd = process.cwd();
  // 需要创建的目录地址
  const targetAir = path.join(cwd, name);
  // 目录是否已经存在？
  if (fs.existsSync(targetAir)) {
    const answers = await inquirer.prompt([
      {
        type: "confirm",
        message: "目标目录存在。你想把它覆盖掉吗？",
        name: "ok",
      },
    ]);
    if (answers.ok) {
      spinner.start("正在删除...");
      await fs.remove(targetAir);
      spinner.succeed("已删除");
      await create(name, options);
    }
  } else {
    //创建项目
    const generator = new Generator(name, targetAir);
    generator.create();
  }
};
